package com.brillilab.service.job.user;

import com.brillilab.common.utils.DateUtil;
import com.brillilab.domain.enums.lab.InviteCodeTypeEnum;
import com.brillilab.domain.po.lab.Lab;
import com.brillilab.domain.po.user.InviteCode;
import com.brillilab.domain.po.user.Users;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.lab.ILabService;
import com.brillilab.service.core.user.IInviteCodeService;
import com.brillilab.service.core.user.IUsersService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.service.module.message.producer.MessageProducer;
import com.brillilab.service.quartz.BrillQuartzScheduler;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zlb
 * @Title: MakeInviteCodeJob
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/10/25 11:28
 */
@Component
public class MakeInviteCodeJob  implements Job {
    private static Logger logger = LoggerFactory.getLogger(MakeInviteCodeJob.class);

    @Resource
    private BrillQuartzScheduler scheduler;
    @Resource
    private IUsersService usersService;
    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ILabMemberService labMemberService;
    @Resource
    private ILabService labService;
    @Resource
    private IInviteCodeService inviteCodeService;
    @Resource
    private MessageProducer producer;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        long start = System.currentTimeMillis();
        logger.info("邀请码定时发放任务执行开始..." + start);
        List<Lab> labList = labService.selectAllLabList();
        if (!CollectionUtils.isEmpty(labList)) {
            List<Users> usersList = usersService.selectAllUserList();
            for (Lab lab : labList) {
                Date makeDate = DateUtil.getBeginDayOfMonth();//月初第一天
                Date expireDate =DateUtil.stringToDate(DateUtil.dateString(DateUtil.getEndDayOfMonth(makeDate), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
                List<InviteCode> inviteCodeInfo = inviteCodeService.getInviteCodeByMakeTime(lab.getId(), expireDate);
                if (CollectionUtils.isEmpty(inviteCodeInfo)) {
                    //生成邀请码
                    Users users = usersList.stream().filter(p -> p.getId().equals(lab.getOwnerId())).findAny().orElse(null);
                    if(users!=null) {
                        logger.info("推荐码生成-UserID：" + users.getId());
                        inviteCodeService.generateInviteCode(InviteCodeTypeEnum.USER_INVITE.getName(), 3,lab.getId(), users, expireDate);
                    }
                }
            }
        }
        logger.info("邀请码定时发放任务执行结束" + System.currentTimeMillis());
        logger.info("邀请码定时发放任务执行耗时：" + (System.currentTimeMillis() - start));
    }
}
